package com.qige.dome.homework;


/**
 * @Author qige
 * @Date 2024-08-07 18:49
 * @Description
 */
public class homework4 {
    /*
    1.设计一个多线程的程序如下：
    设计一个火车售票模拟程序。假如火车站要有100张火车票要卖出，现在有5个售票点同时售票，用5个线程模拟这5个售票点的售票情况。
     */
    public static void main(String[] args) {
        ThreadSale threadSale = new ThreadSale();
        Thread thread1 = new Thread(threadSale,"窗口1");
        Thread thread2 = new Thread(threadSale,"窗口2");
        Thread thread3 = new Thread(threadSale,"窗口3");
        Thread thread4 = new Thread(threadSale,"窗口4");
        Thread thread5 = new Thread(threadSale,"窗口5");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();

    }
}
class ThreadSale implements Runnable{
    private int ticket = 100;
    private int count = 0;
    public ThreadSale() {
    }

    public ThreadSale(int ticket, int count) {
        this.ticket = ticket;
        this.count = count;
    }

    @Override
    public void run() {
        while (ticket > 0){
            synchronized (this){
                if (ticket > 0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    count++;
                    ticket--;
                    System.out.println(Thread.currentThread().getName()+"卖出了第"+count+"张票，还剩下"+ticket+"张票");
                }
            }
        }
    }
}